/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FFTAnalysis;

import java.beans.*;
import java.io.File;
import java.io.Serializable;
import flanagan.io.*;

/** @Brief A component that sets properties for a wave file
 *
 * @author Grant
 * 
 * 
 */
public class ConvertingBean implements Serializable {
    
    private String inputFile;
    private int numChannels;//1
    private int validBits;//8
    private long sampleRate;//8000
    private int buf_size;// = 5001;
    private String outputFile;
    
    private static final int NUMCHANNELS = 1;
    private static final int VALIDBITS = 8;
    private static final int BUF_SIZE = 5001;
    private static final int SAMPLERATE = 8000;
    private String flanagan;
    
    
    /** @brief default constructor
     * 
     *  Constructs a default converting bean
     */
    public ConvertingBean()
    {
        inputFile = "";
        numChannels = NUMCHANNELS;
        validBits = VALIDBITS;
        sampleRate = SAMPLERATE;
        buf_size = BUF_SIZE;
        outputFile = "";
    }
    
    /**@brief sets the inputFile path
     * @param newInputPath the new Input Path
     */    
    public void setInputFile(String newInputPath) 
    {
        inputFile = newInputPath;
        this.doConversion();
    }
    
    /**@brief returns the inputFile
     * @return the inputFile Path
     */    
    public String getInputFile() 
    {
        return inputFile;
    }
    
    /**@brief returns the outputFile
     * @return the inputFile Path
     */    
    public String getOutputFile() 
    {
        return flanagan;
    }
    
    
    /**
     * need to put in  method GUIProcessedOutput(audiofilein)
     */
        
    /**
    * @brief convert the inputFile
    * 
    * @description The input file will be converted to the desired sample rate, 
    * number of channels and valid bits
    *  
    * ref: http://docs.oracle.com/javase/tutorial/sound/converters.html
    */
    public void doConversion()
    {
    try
        {
            File in = new File(inputFile);
            WavFile readWavFile = WavFile.openWavFile(in);            
            outputFile = in.getParentFile().toString();             
            int length = in.getName().length();
            String name = in.getName().substring(0, length - 4);
            outputFile = outputFile.concat("\\").concat(name).concat("_processed.wav");
            //System.out.println(ofname);   //debugging only
            WavFile writeWavFile = WavFile.newWavFile(new File(outputFile), numChannels, readWavFile.getNumFrames(), validBits, sampleRate);

            double[] buffer = new double[BUF_SIZE * 2];

            int framesRead = 0;
            int framesWritten = 0;

            do
            {
                    framesRead = readWavFile.readFrames(buffer, BUF_SIZE);
                    framesWritten = writeWavFile.writeFrames(buffer, BUF_SIZE);
                    //System.out.printf("%d %d\n", framesRead, framesWritten);
                    }
            while (framesRead != 0);

            readWavFile.close();
            writeWavFile.close();
         }
            catch (Exception e)
            {
                System.err.println(e);
                e.printStackTrace();
            }
     }  
    
    
    
}
